﻿using System;
using System.Data;
using System.Text;
using System.Web;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references

namespace Lawyer.DAL
{
	/// <summary>
	/// 数据访问类:PostInfo
	/// </summary>
	public partial class PostInfo
	{
		public PostInfo()
		{}
		#region  Method

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(string PostID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from PostInfo");
			strSql.Append(" where PostID=@PostID ");
			SqlParameter[] parameters = {
					new SqlParameter("@PostID", SqlDbType.VarChar,36)};
			parameters[0].Value = PostID;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public bool Add(Lawyer.Model.PostInfo model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into PostInfo(");
			strSql.Append("PostID,Tile,Detail,Status,CreatedBy,CreatedDate,UpdatedBy,UpdatedDate)");
			strSql.Append(" values (");
			strSql.Append("@PostID,@Tile,@Detail,@Status,@CreatedBy,@CreatedDate,@UpdatedBy,@UpdatedDate)");
			SqlParameter[] parameters = {
					new SqlParameter("@PostID", SqlDbType.VarChar,36),
					new SqlParameter("@Tile", SqlDbType.VarChar,50),
					new SqlParameter("@Detail", SqlDbType.Text),
					new SqlParameter("@Status", SqlDbType.Int,4),
					new SqlParameter("@CreatedBy", SqlDbType.VarChar,36),
					new SqlParameter("@CreatedDate", SqlDbType.DateTime),
					new SqlParameter("@UpdatedBy", SqlDbType.VarChar,36),
					new SqlParameter("@UpdatedDate", SqlDbType.DateTime)};
			parameters[0].Value = model.PostID;
			parameters[1].Value = model.Tile;
			parameters[2].Value = model.Detail;
			parameters[3].Value = model.Status;
			parameters[4].Value = model.CreatedBy;
			parameters[5].Value = model.CreatedDate;
			parameters[6].Value = model.UpdatedBy;
			parameters[7].Value = model.UpdatedDate;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(Lawyer.Model.PostInfo model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update PostInfo set ");
			strSql.Append("Tile=@Tile,");
			strSql.Append("Detail=@Detail,");
			strSql.Append("Status=@Status,");
			strSql.Append("CreatedBy=@CreatedBy,");
			strSql.Append("CreatedDate=@CreatedDate,");
			strSql.Append("UpdatedBy=@UpdatedBy,");
			strSql.Append("UpdatedDate=@UpdatedDate");
			strSql.Append(" where PostID=@PostID ");
			SqlParameter[] parameters = {
					new SqlParameter("@Tile", SqlDbType.VarChar,50),
					new SqlParameter("@Detail", SqlDbType.Text),
					new SqlParameter("@Status", SqlDbType.Int,4),
					new SqlParameter("@CreatedBy", SqlDbType.VarChar,36),
					new SqlParameter("@CreatedDate", SqlDbType.DateTime),
					new SqlParameter("@UpdatedBy", SqlDbType.VarChar,36),
					new SqlParameter("@UpdatedDate", SqlDbType.DateTime),
					new SqlParameter("@PostID", SqlDbType.VarChar,36)};
			parameters[0].Value = model.Tile;
			parameters[1].Value = model.Detail;
			parameters[2].Value = model.Status;
			parameters[3].Value = model.CreatedBy;
			parameters[4].Value = model.CreatedDate;
			parameters[5].Value = model.UpdatedBy;
			parameters[6].Value = model.UpdatedDate;
			parameters[7].Value = model.PostID;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(string PostID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from PostInfo ");
			strSql.Append(" where PostID=@PostID ");
			SqlParameter[] parameters = {
					new SqlParameter("@PostID", SqlDbType.VarChar,36)};
			parameters[0].Value = PostID;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string PostIDlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from PostInfo ");
			strSql.Append(" where PostID in ("+PostIDlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Lawyer.Model.PostInfo GetModel(string PostID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 PostID,Tile,Detail,Status,CreatedBy,CreatedDate,UpdatedBy,UpdatedDate from PostInfo ");
			strSql.Append(" where PostID=@PostID ");
			SqlParameter[] parameters = {
					new SqlParameter("@PostID", SqlDbType.VarChar,36)};
			parameters[0].Value = PostID;

			Lawyer.Model.PostInfo model=new Lawyer.Model.PostInfo();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["PostID"]!=null && ds.Tables[0].Rows[0]["PostID"].ToString()!="")
				{
					model.PostID=ds.Tables[0].Rows[0]["PostID"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Tile"]!=null && ds.Tables[0].Rows[0]["Tile"].ToString()!="")
				{
					model.Tile=ds.Tables[0].Rows[0]["Tile"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Detail"]!=null && ds.Tables[0].Rows[0]["Detail"].ToString()!="")
				{
					model.Detail=HttpUtility.HtmlDecode( ds.Tables[0].Rows[0]["Detail"].ToString());
				}
				if(ds.Tables[0].Rows[0]["Status"]!=null && ds.Tables[0].Rows[0]["Status"].ToString()!="")
				{
					model.Status=int.Parse(ds.Tables[0].Rows[0]["Status"].ToString());
				}
				if(ds.Tables[0].Rows[0]["CreatedBy"]!=null && ds.Tables[0].Rows[0]["CreatedBy"].ToString()!="")
				{
					model.CreatedBy=ds.Tables[0].Rows[0]["CreatedBy"].ToString();
				}
				if(ds.Tables[0].Rows[0]["CreatedDate"]!=null && ds.Tables[0].Rows[0]["CreatedDate"].ToString()!="")
				{
					model.CreatedDate=DateTime.Parse(ds.Tables[0].Rows[0]["CreatedDate"].ToString());
				}
				if(ds.Tables[0].Rows[0]["UpdatedBy"]!=null && ds.Tables[0].Rows[0]["UpdatedBy"].ToString()!="")
				{
					model.UpdatedBy=ds.Tables[0].Rows[0]["UpdatedBy"].ToString();
				}
				if(ds.Tables[0].Rows[0]["UpdatedDate"]!=null && ds.Tables[0].Rows[0]["UpdatedDate"].ToString()!="")
				{
					model.UpdatedDate=DateTime.Parse(ds.Tables[0].Rows[0]["UpdatedDate"].ToString());
				}
				return model;
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select PostID,Tile,Detail,Status,CreatedBy,CreatedDate,UpdatedBy,UpdatedDate ");
			strSql.Append(" FROM PostInfo ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" PostID,Tile,Detail,Status,CreatedBy,CreatedDate,UpdatedBy,UpdatedDate ");
			strSql.Append(" FROM PostInfo ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "PostInfo";
			parameters[1].Value = "PostID";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  Method
	}
}

